package com.ophiux.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 说明：微信证书测试<br>
 * <h1>创 建 人: hehailong<br></h1>
 * 创建日期: 2018年7月9日 下午1:36:19<br>
 * 需要的jar包:
 */
public class TestServlet extends HttpServlet{
	private static Logger logger = LoggerFactory.getLogger(TestServlet.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String appid="wx394240ecef75c40a";//PropertyUtil.getResWxValue("appid");
//		String appsecret="9a4c4cf7d9616541c117e2204afafe35";//PropertyUtil.getResWxValue("appsecret");
//		String mch_id="1453527302";//PropertyUtil.getResWxValue("mch_id");
		String pay_key="2017OPHIUXabcophiux123456XUIHPO1";
		
//		String appid="wx7a912aa164e727b7";//PropertyUtil.getResWxValue("appid");
//		String appsecret="7b2fb19d4ebb6840bf3de9cdf881d363";//PropertyUtil.getResWxValue("appsecret");
//		String mch_id="1486444972";//PropertyUtil.getResWxValue("mch_id");
		
		String appid=PropertyUtil.getResWxValue("appid");
		String appsecret=PropertyUtil.getResWxValue("appsecret");
		String mch_id=PropertyUtil.getResWxValue("mch_id");
		
		String nonce_str="123123123123";
		String sign_type="MD5";
		
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("appid", appid);
		paramMap.put("nonce_str", nonce_str);
		paramMap.put("pay_key",pay_key );
		paramMap.put("sign_type", sign_type);
		paramMap.put("mch_id", mch_id);
		String sign = "";
		try {
			sign = SignUtils.getSign(paramMap, appsecret);
		} catch (Exception e1) {
			e1.printStackTrace();
			logger.info("获取签名异常",e1);
		}
		
		
		String StrParam = "<xml><sign>"+sign+"</sign><mch_id>"+mch_id+"</mch_id><pay_key>"+pay_key+"</pay_key><sign_type>"+sign_type+"</sign_type>"+"<appid>"+appid+"</appid><nonce_str>"+nonce_str+"</nonce_str></xml>";
		
		StrParam = "<xml><sign>A14E584C94CDEDA4B75FF85750440BEB</sign><mch_id>1453527302</mch_id><sign_type>MD5</sign_type><appid>wx394240ecef75c40a</appid><nonce_str>123123123123</nonce_str></xml>";
		
		System.out.println("微信请求参数："+StrParam);
		String dd = "";
		String ddd= "";
		try {
//			dd = HttpUtil.httpsRequest("https://apitest.mch.weixin.qq.com/sandboxnew/pay/getsignkey", "POST", StrParam, 3000);
//			System.out.println("微信测试接口，返回结果:"+dd);
//			logger.info("微信测试接口，返回结果:"+dd);
			
			
			ddd = HttpUtil.httpsRequest("https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey", "POST", StrParam, 3000);
			System.out.println("微信正式接口，返回结果:"+ddd);
			logger.info("微信正式接口，返回结果:"+ddd);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.setCharacterEncoding("UTF-8");
		response.getWriter().println("沙箱:"+dd);
		response.getWriter().println("正式:"+ddd);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
